<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">确认</button>
</body>
<script>
    let btn = document.getElementById('btn')
    function handle() {
        console.log('向后端发请求');

    }
    btn.addEventListener('click', throttle(handle, 2000))
    //截流
    function throttle(fn, wait) {
        let preTime = 0
        return function (...arg) {//将参数集合成一个arg数组
            let nowTime = Date.now()//第一次点击的时间戳
            if (nowTime - preTime >= wait) {
                //当时间过了两秒以上时就执行fn，并将时间初始值更新，以便后续计算时间间隔
                fn.call(this, ...arg)//将fn内的this指向funcion内的this，并将参数arg数组解构出来
                preTime = nowTime
            }

        }
    }
</script>

</html>